package com.common.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.common.dao.PrivilegeDAO;
import com.common.dao.RoleDAO;
import com.common.dao.RolePrivilegeDAO;
import com.common.dto.PrivilegeDTO;
import com.common.service.PrivilegeService;
import com.common.service.base.BaseService;
import com.common.util.ConstantDefine;
import com.common.util.ShiroUtil;
import com.github.pagehelper.PageInfo;

@Service
public class PrivilegeServiceImpl extends BaseService<PrivilegeDTO> implements PrivilegeService {
	@Autowired
	private PrivilegeDAO privilegeDao;
	@Autowired
	private RoleDAO roleDao;
	@Autowired
	private RolePrivilegeDAO rolePrivilegeDao;

	@Override
	public Map<String, Object> check(PrivilegeDTO entity) {
		// TODO Auto-generated method stub
		return null;
	}

    @Override
    public Map<String, Object> checkUpdate(PrivilegeDTO entity) {
        return null;
    }


    /**
     * 获取1,2,3级
     * Created by wrl on 2017/07/11
     * @param entity
     * @return
     */
    @Override
    public Map<String, Object> selectByGraded(PrivilegeDTO entity) {
		Map<String, Object> map = new HashMap<>();
        entity.setFunctionType("1");
        //first
        List<PrivilegeDTO> firstList = privilegeDao.selectByDTO(entity);

        List<PrivilegeDTO> firstResultList = new ArrayList<>();
        for(Iterator<PrivilegeDTO> iterator = firstList.iterator(); iterator.hasNext(); ){
            PrivilegeDTO dto = iterator.next();
            entity.setParentFunctionCode(dto.getFunctionCode());
            entity.setFunctionType("2");
            //second
            List<PrivilegeDTO> secondList = privilegeDao.selectByDTO(entity);

            List<PrivilegeDTO> secondResultList = new ArrayList<>();
            for(Iterator<PrivilegeDTO> iterator2 = secondList.iterator(); iterator2.hasNext(); ){
                PrivilegeDTO dto2 = iterator2.next();
                entity.setParentFunctionCode(dto2.getFunctionCode());
                entity.setFunctionType("3");
                //third
                List<PrivilegeDTO> thirdList = privilegeDao.selectByDTO(entity);

                //3->2
                dto2.setChildList(thirdList);
                secondResultList.add(dto2);
            }
            //2->1
            dto.setChildList(secondResultList);
            firstResultList.add(dto);
        }
		map.put("list",firstResultList);
        return map;
    }

    /**
     * 获取1,2级
     * Created by wrl on 2017/09/14
     * @param entity
     * @return
     */
    @Override
    public Map<String, Object> selectTwo(PrivilegeDTO entity) {
		Map<String, Object> map = new HashMap<>();
        entity.setFunctionType("1");
        //first
        List<PrivilegeDTO> firstList = privilegeDao.selectByDTO(entity);

        List<PrivilegeDTO> firstResultList = new ArrayList<>();
        for(Iterator<PrivilegeDTO> iterator = firstList.iterator(); iterator.hasNext(); ){
            PrivilegeDTO dto = iterator.next();
            entity.setParentFunctionCode(dto.getFunctionCode());
            entity.setFunctionType("2");
            //second
            List<PrivilegeDTO> secondList = privilegeDao.selectByDTO(entity);

            //2->1
            dto.setChildList(secondList);
            firstResultList.add(dto);
        }
		map.put("list",firstResultList);
        return map;
    }

    /**
     * Created by wrl on 2017/07/13
     * @param ids
     * @return
     */
    @Override
    public Map<String, Object> deleteByIds(List<String> ids) {
        Map<String, Object> map = new HashMap<>();
        int count = privilegeDao.deleteByIds(ids);
        if(count != 0 ){
            map.put(ConstantDefine.FLAG,true);
        }
        return map;
    }

	@Override
	public PageInfo<PrivilegeDTO> selectCustomPaging(PrivilegeDTO entity) {
		// TODO Auto-generated method stub
		return null;
	}

	/**
	 * 条件查询
	 */
	@Override
	public Map<String, Object> selectFunctionPaging(PrivilegeDTO privilegeDTO) {
        List<PrivilegeDTO> list = new ArrayList<>();
        String functionName = privilegeDTO.getFunctionName();
        if("1".equals(privilegeDTO.getIsSearch()))
            list = privilegeDao.search(privilegeDTO);
        else
            list=privilegeDao.selectByDTO(privilegeDTO);
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put(ConstantDefine.ROWS, list);
        return resultMap;
	}

    @Override
    public Map<String, Object> selectAuthorityByType(PrivilegeDTO entity) {
        Map<String, Object> map = new HashMap<>();
        String personCode = (String)ShiroUtil.getSessionByKey(ConstantDefine.PERSONCODE_SESSION);
        entity.setPersonCode(personCode);
        entity.setFunctionType("1");
        //first
        List<PrivilegeDTO> firstList = privilegeDao.selectAuthorityByType(entity);

        List<PrivilegeDTO> firstResultList = new ArrayList<>();
        for(Iterator<PrivilegeDTO> iterator = firstList.iterator(); iterator.hasNext(); ){
            PrivilegeDTO dto = iterator.next();
            entity.setParentFunctionCode(dto.getFunctionCode());
            entity.setFunctionType("2");
            //second
            List<PrivilegeDTO> secondList = privilegeDao.selectAuthorityByType(entity);

            //2->1
            dto.setChildList(secondList);
            firstResultList.add(dto);
        }
        map.put("list",firstResultList);
         return map;
    }
    
    /**
     * 查询button权限
     * @return
     */
    public List<String> selectButtonAuthority() {
    	PrivilegeDTO entity = new PrivilegeDTO();
        String personCode = (String)ShiroUtil.getSessionByKey(ConstantDefine.PERSONCODE_SESSION);
        entity.setPersonCode(personCode);
        entity.setFunctionType("3");
        //查询button权限
        List<PrivilegeDTO> firstList = privilegeDao.selectAuthorityByType(entity);
    	//返回所有权限码
    	List<String> priviList = new ArrayList<>(firstList.size());
        for(Iterator<PrivilegeDTO> iterator = firstList.iterator(); iterator.hasNext(); ){
        	PrivilegeDTO privilegeDTO = iterator.next();
        	priviList.add(privilegeDTO.getPermissionCode());
        }
        return priviList;
	}
}
